<?php
//
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) All rights reserved.                                     |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有！                         |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0                                                         |
// +------------------------------------------------------------------------+
// | Author: anguolei@clcw.com.cn                                           |
// +------------------------------------------------------------------------+
//
namespace AuctionAppApi\Controller;

use AuctionCore\CarResource\CarModel;

/**
 * 历史车价查询
 */
class HisPriceController extends BaseController
{

    public function __construct()
    {
        parent::__construct();
    }

    public function get_list()
    {
        $page = I('post.page/d', 1);
        $page_size = I('post.page_size/d', 20);
        $map = $this->get_map();
        //计算记录数
        $total = M('order')->alias('o')->join(C('DB_PREFIX') . "cars c On o.car_id=c.car_id")->where($map)->count();

        $cols = "o.order_id,o.success_price,o.trade_time,o.car_id,c.pointer_mileage,c.emission,c.brand_id,c.series_id,
            c.model_id,c.first_reg_date,c.composite_state,c.accident_level,c.car_color,c.transfer_limit,c.plate_prefix,c.plate_suffix,c.car_cc,c.turbo,c.gearbox,c.location_area";
        $res = M('order')->alias('o')->field($cols)->join(C('DB_PREFIX') . "cars c On o.car_id=c.car_id")
            ->where($map)->order('o.order_id DESC')->page($page)->limit($page_size)->select();
        $cm = new CarModel();
        $a = new \AuctionCore\Other\Address();
        $list = array();
        foreach ($res as $k => $val) {
            $list[$k]['car_id'] = $val['car_id'];
            $series = $cm->get_model_by_id($val['series_id'], 'series');
            $city_name = $a->get_name_by_code($val['location_area']);

            $list[$k]['car_name'] = '['.$city_name.'] '.$series['name'] . ' ' . $val['car_cc'] . ($val['turbo'] ? 'T' : 'L') . ' ' . C('GEARBOX.' . $val['gearbox']);
            $desc = array();
            $desc['first_reg_date'] = date('Y年m月', strtotime($val['first_reg_date'])) . "初登";
            $desc['pointer_mileage'] = convert_mileage($val['pointer_mileage']) . "万公里";
            $desc['emission'] = C("EMISSION." . $val['emission']);
            $desc['plate_number'] = C('PLATE_PREFIX.' . $val['plate_prefix']) . substr($val['plate_suffix'], 0, 1);
            $desc['transfer_limit'] =  C('TRANSFER_LIMIT.' . $val['transfer_limit']);
            $desc['composite_level'] = $val['composite_state'] . $val['accident_level'];
            $list[$k]['desc'] = implode("/", $desc);
            $list[$k]['price'] = (int)$val['success_price'];
            $list[$k]['trade_time'] = $val['trade_time'];
        }

        $data = array();
        $data['currentpage'] = $page;
        $data['pagesize'] = $page_size;
        $data['maxcount'] = $total;
        $data['maxpage'] = ceil($total / $page_size);
        $data['list'] = $list;
        $this->api_200($data);
    }


    public function get_count()
    {
        $map = $this->get_map();
        $total = M('order')->alias('o')->join(C('DB_PREFIX') . "cars c On o.car_id=c.car_id")->where($map)->count();
        $this->api_200($total);
    }

    private function get_map()
    {
        $mbrand_id = I('post.mbrand_id');//主品牌ID
        $series_id = I('post.series_id');//车系ID
        $emission_std = I('post.emission');//排放标准（多选）
        $emission_std = empty($emission_std) ? array() : explode(',', $emission_std);
        $price_range = I('post.prices_interval');//推荐价格区间（多选）
        $price_range = empty($price_range) ? array() : explode(',', $price_range);
        $custom_low_price = I('post.custom_low_price');//自定义最低价
        $custom_high_price = I('post.custom_high_price');//自定义最高价
        $age_range = I('post.year_interval');//年限（多选)
        $age_range = empty($age_range) ? array() : explode(',', $age_range);
        $condition_level = I('post.condition_level');//车况等级（多选）
        //条件
        $map = array();
        $map['o.status'] = array('eq', '13');

        if ($mbrand_id) {
            $map['c.mbrand_id'] = array('eq', $mbrand_id);
        }
        if ($series_id) {
            $map['c.series_id'] = array('eq', $series_id);
        }
        //排放标准
        if ($emission_std && is_array($emission_std)) {
            if (!in_array('-1', $emission_std)) {
                $map['c.emission'] = array('in', $emission_std ? $emission_std : array(0));
            }
        }
        //价格范围
        $custom_low_price = $custom_low_price * 10000;
        $custom_high_price = $custom_high_price * 10000;
        if ($custom_low_price && !$custom_high_price) {
            $map['o.success_price'] = array('egt', $custom_low_price);
        } elseif (!$custom_low_price && $custom_high_price) {
            $map['o.success_price'] = array('elt', $custom_high_price);
        } elseif ($custom_low_price && $custom_high_price) {
            $map['o.success_price'] = array(array('egt', $custom_low_price), array('elt', $custom_high_price));
        } else {
            if ($price_range && is_array($price_range) && !in_array('-1', $price_range)) {
                $where = array();
                $arr = array();
                foreach ($price_range as $k => $v) {
                    switch ($v) {
                        case '1':
                            $arr[] = "(o.success_price < '50000')";
                            break;
                        case '2':
                            $arr[] = "(o.success_price >= '50000' AND o.success_price <= '100000')";
                            break;
                        case '3':
                            $arr[] = "(o.success_price >= '100000' AND o.success_price <= '200000')";
                            break;
                        case '4':
                            $arr[] = "(o.success_price > '200000')";
                            break;
                    }
                }
                $where['_string'] = implode(' OR ', $arr);
                //$where['_logic'] = 'or';
                $map['_complex'] = $where;
            }
        }
        //使用年限
        if ($age_range && is_array($age_range) && !in_array('-1', $age_range)) {
            $arr = array();
            foreach ($age_range as $k => $v) {
                switch ($v) {
                    case '1':
                        $arr[] = "(c.first_reg_date > '" . date('Y-m-d', strtotime('-3 year')) . "')";
                        break;
                    case '2':
                        $arr[] = "(c.first_reg_date >= '" . date('Y-m-d', strtotime('-6 year')) . "' AND c.first_reg_date <= '" . date('Y-m-d', strtotime('-3 year')) . "')";
                        break;
                    case '3':
                        $arr[] = "(c.first_reg_date > '" . date('Y-m-d', strtotime('-10 year')) . "' AND c.first_reg_date <= '" . date('Y-m-d', strtotime('-6 year')) . "')";
                        break;
                    case '4':
                        $arr[] = "(c.first_reg_date <=   '" . date('Y-m-d', strtotime('-10 year')) . "')";
                        break;
                }
            }
            $map[1] = implode(' OR ', $arr);
        }
        $condition_level = empty($condition_level) ? array() : explode(',', $condition_level);
        //车况等级
        if ($condition_level && is_array($condition_level) && !in_array('-1',$condition_level)) {
            $com_state = array();
            $car_level = array();
            foreach ($condition_level as $value) {
                preg_match("/([0-9]+)([a-zA-Z]+)/", $value, $matches);
                $com_state[] = $matches[1];
                $car_level[] = $matches[2];
            }
            //综合车况
            if ($com_state) {
                $map['c.composite_state'] = array('in', $com_state ? array_unique($com_state) : array(0));
            }
            //事故等级
            if ($car_level) {
                $map['c.accident_level'] = array('in',$car_level?array_unique($car_level):array(0));
            }
        }
        return $map;
    }
}